import {getCommentList,commentLike,commentUnlike} from "@/api/api";
import {addComment,addFav,removeFav,articleLike,articleUnike} from "@/api/api";
import { Toast } from 'vant';

export default{
    //获取文章详情
    getArticleDetail() {
        this.$http
          .post("/api/get_article_detail", {
            article_id: this.article_id,
            user_id: this.user_id,
          })
          .then((res) => {
            console.log(res);
            this.text = res.data.data;
          });
      },
      //获取评论列表
      getCommentList(){
        let {user_id,article_id,skip,limit} = this;
        getCommentList(
          {article_id,skip,limit,user_id}
        ).then(res=>{
          console.log(res);
          this.commentList=res.data.data
          console.log(this.commentList)
         
        })
      },
      //评论文章
      addComment(){
        let {article_id,comment_type,content,reply_comment_id} = this;
        addComment(
          {user_id:this.uid,article_id,comment_type,content,reply_comment_id}
          ).then(res=>{
            console.log(res);
            this.skip=0;
             this.commentList=[];
             this.getCommentList();
            if(res.data.code===0){
             Toast(res.data.msg)
             this.show=false
             this.content='';
             
            }else if(this.content===''){
              Toast("评论失败，不能为空")
            }
            
          })
  
      },
      //收藏
      collect(){
  
      let {article_id,user_id,uid} = this;
      if(!uid){
          Toast('请先登录');
          setTimeout((v)=>{
            this.$router.push("/login")
          });
          return
        }
      //  if(!this.checkLogin()) return 
       if(this.text.is_fav){
        removeFav(
          {article_id,user_id}
          ).then((res)=>{
            console.log(res)
            Toast(res.data.msg);
            if(res.data.code==0){
              this.text.is_fav=false
            }
          })
  
       }else{
        addFav(
          {article_id,user_id}
        ).then((res)=>{
          console.log(res)
          Toast(res.data.msg);
          if(res.data.code==0){
            this.text.is_fav=true
          }
        })
       }
  
      },
  
      //文章点赞
      
      articleLike(){
        // console.log(this.uid)
        // if(this.checkLogin()) return 
        
        let {article_id,user_id,uid} = this;
        if(!uid){
          Toast('请先登录');
          setTimeout((v)=>{
            this.$router.push("/login")
          });
          return
        }
        console.log(uid)
        if(this.text.is_like){
          articleUnike(
            {article_id,user_id}
          ).then((res)=>{
            console.log(res)
            Toast(res.data.msg);
            if(res.data.code==0){
            this.text.is_like=false
          }
          })
        }else{
          articleLike(
            {article_id,user_id}
          ).then((res)=>{
            console.log(res)
            Toast(res.data.msg);
             if(res.data.code==0){
            this.text.is_like=true
          }
          })
        }
      },
  
      //评论点赞
      commentLike(comment_id,is_like,index){
        let {uid,user_id} = this;
        if(!uid){
          Toast('请先登录');
          setTimeout((v)=>{
            this.$router.push("/login")
          });
          return
        }
        
        if(is_like){
          commentUnlike(
            {comment_id,user_id}
          ).then((res)=>{
            console.log(res);
            Toast(res.data.msg);
            if(res.data.code==0){
              this.commentList[index].like_count=res.data.count
              this.commentList[index].is_like=false
            }
            
          })
  
        }else{
          commentLike(
            {comment_id,user_id}
          ).then((res)=>{
            console.log(res);
            Toast(res.data.msg);
             if(res.data.code==0){
              this.commentList[index].like_count=res.data.count
              this.commentList[index].is_like=true
            }
            
          })
        }
      },
  
}